package cn.netjava.wk;


import java.io.*;

public class FileCopy {
	String srcdir, destdir = null;
	public FileCopy(String srcdir, String destdir) {
		this.srcdir = srcdir;
		this.destdir = destdir;	    
	}
	/*
	 * this method can copy a source file to a destination place in this
	 * process,it will create a new destination file then copy the content
	 * @value: String srcdirfile,String destdirfile @return null
	 */
	public void canCopy(){		
	  File  fileexits=new File(srcdir);
	    if(fileexits.exists()==false){
	    	System.out.println("The source does not exits!");
	       return ;
	    }
	  if(fileexits.isFile()){
		  copyFile(srcdir,destdir);
	  }else if(fileexits.isDirectory()){
		  copyDir(srcdir,destdir);
	  }else{
		  return ;
	  };
      FileSearch copyfileok=new FileSearch(destdir,fileexits.getName());
	   if( copyfileok.searchFile(destdir,fileexits.getName())){
		   System.out.println("Copying has successed! ");
	       return ;
	   }else{
		  System.out.println("Copying has failure!");
		   return ;
	   }
    
	}
	public void copyFile(String srcdirfile, String destdirfile) {
		try {
			FileInputStream filestream = new FileInputStream(new File(
					srcdirfile));
			File destfile = new File(destdirfile+File.separator
					+ new File(srcdirfile).getName());
			try {
				destfile.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
				return;
			}
			FileOutputStream deststream = new FileOutputStream(destfile);
			BufferedInputStream buffsrc = new BufferedInputStream(filestream);
			BufferedOutputStream buffdest = new BufferedOutputStream(deststream);
			try {
				int i = -1;
				while ((i = buffsrc.read()) != -1) {
					buffdest.write(i);
				}

				buffdest.flush();
				buffdest.close();
				deststream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return;
		}
	}

	/*
	 * this method can copy a directory to a point place in this process,it will
	 * create a new directory then copy the file content
	 * 
	 * @see copyFile(String srcdirfile,String destdirfile) @value String
	 *      srcdir,String destdir @return null
	 */

	public void copyDir(String srcdir, String destdir) {
		File srcdire = new File(srcdir);
		File[] subsrcdire = srcdire.listFiles();
		File ff = new File(destdir +File.separator+srcdire.getName()+File.separator);
		ff.mkdir();
		
		if (subsrcdire != null) {
			for (int i = 0; i < subsrcdire.length; i++) {
				if (subsrcdire[i].isFile()) { 
					
					copyFile(subsrcdire[i].getAbsolutePath(), ff.getAbsolutePath());
							
							
				} else if (subsrcdire[i].isDirectory()) {
					File subdestdire = new File(ff.getAbsolutePath()+File.separator+subsrcdire[i].getName()+File.separator);
							
					subdestdire.mkdir();
					copyDir(subsrcdire[i].getAbsolutePath(), subdestdire.getParentFile().getAbsolutePath());
							
				}

			}
		}
	}

//	public static void main(String[] args) {
//		FileCopy copy = new FileCopy("D:\\tt\\wpp.java", "E:\\pp\\wpp.java");
//		// copy.copyFile("D:\\tt\\wpp.java","E:\\");
//		copy.copyDir("D:\\Tomcat 5.0", "E:\\");
//	}
}
